package gui.reports.productstats;

import gui.common.*;

import java.io.File;
import java.io.IOException;
import java.util.Observer;
import java.util.Observable;

import reports.builders.HTMLBuilder;
import reports.builders.IBuilder;
import reports.builders.PDFBuilder;
import reports.directors.ProductStatisticsReport;

/**
 * Controller class for the product statistics report view.
 */
public class ProductStatsReportController extends Controller implements
		IProductStatsReportController, Observer {

	private FileFormat format;
	private int months;
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the item statistics report view
	 */
	public ProductStatsReportController(IView view) {
		super(view);
		
		format = this.getView().getFormat();
		
		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IProductStatsReportView getView() {
		return (IProductStatsReportView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{
		this.getView().enableOK(true);
		
		String strMonths = this.getView().getMonths();
		
		int intMonths;
		try
		{ 	
			intMonths = new Integer(strMonths);
			months = intMonths;
		}
		catch (NumberFormatException e)
		{	intMonths = -1;	}
		
		if (intMonths < 1 || intMonths > 100)
			this.getView().enableOK(false);
		
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() 
	{
		months = 3;
		this.getView().setMonths("3");
	}

	//
	// IProductStatsReportController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * product statistics report view is changed by the user.
	 */
	@Override
	public void valuesChanged()
	{
		format = this.getView().getFormat();
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the product statistics report view.
	 */
	@Override
	public void display() 
	{
		IBuilder builder;
		
		if (format == FileFormat.HTML)
			builder = new HTMLBuilder("Product Statistics");
		else
			builder = new PDFBuilder("Product Statistics");
		
		ProductStatisticsReport report = new ProductStatisticsReport(months);
		report.build(builder);
		
		String path = builder.getFilePath();
		
		try
		{
			java.awt.Desktop.getDesktop().open(new File(path));
		}
		catch (Exception e)
		{
			getView().displayErrorMessage("Could not open file to display report");
			// e.printStackTrace();
		}
		
		
		
	}

	public void update(Observable obs, Object obj){
	
	}
	
	
}

